package View;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import Management.RuleList;
import Management.Source.Source;
import View.CoolSwing.MyMenuItem;
import View.JPanel.JAnimatorPanel;

public class MainUI extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6313979675865254241L;
	public static Source s = new Source();
	private CardLayout cl;
	private JPanel jp;
	private JMenuBar jmb = new JMenuBar();
	private JMenu jmf = new JMenu(s.file);
	private JMenuItem jmiBack = new MyMenuItem(s.back,s.backIcon);
	private JMenuItem jmiForward = new MyMenuItem(s.forward,s.forwardIcon);
	private JMenuItem jmiExit = new MyMenuItem(s.exit, s.exitIcon);
	private JMenu jmo = new JMenu(s.option);
	private JMenuItem jmiOption = new MyMenuItem(s.option, s.optionIcon);
	private JMenu jma = new JMenu(s.about);
	private JMenuItem jmiAbout = new MyMenuItem(s.about, s.aboutIcon);
	
	/**
	 * Construction;
	 */
	public MainUI() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		
    	//jp and cl
		jp = new JPanel();
		add(jp);

		cl = new CardLayout();
		jp.setLayout(cl);
		for (int i = 0; i < new RuleList().getRuleNumber(); i++) {
			JAnimatorPanel jap = new JAnimatorPanel(i);
			String ii = String.valueOf(i);
			System.out.println(ii);
			jp.add(jap,ii);
		}
		
		//MenuBar
		jmb.add(jmf);
		jmb.add(jmo);
		jmb.add(jma);
		jmf.add(jmiBack);
		jmiBack.addActionListener(this);
		jmiBack.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK));
		jmf.add(jmiForward);
		jmiForward.addActionListener(this);
		jmiForward.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
		jmf.add(jmiExit);
		jmiExit.addActionListener(this);
		jmiExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));
		jmo.add(jmiOption);
		
		jma.add(jmiAbout);
		jmiAbout.addActionListener(this);
		jmb.setName("MenuBar");
		setJMenuBar(jmb);
		
		
		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(s.mainFrame);
		setIconImage(((ImageIcon)  s.mainFrameIcon).getImage());
	}
	
//	public static void main(String[] args) {
//		new MainUI();
//	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jmiBack) {
			cl.previous(jp);
		}
		else if (e.getSource() == jmiForward) {
			cl.next(jp);
		}
		else if (e.getSource() == jmiAbout) {
			JOptionPane.showMessageDialog(this,
			    "AnimationManager is a Software to manage your animaiton",
			    "About",
			    JOptionPane.INFORMATION_MESSAGE,
			    s.aboutIcon);
		}
		else if (e.getSource() == jmiExit) {
			System.exit(0);
		}
	}
}
